package com.david.func_test.activity

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import com.david.func_test.R
import com.david.func_test.widget.DiscoverBottomSheetCallback
import com.david.func_test.widget.DiscoverFloatWidget
import com.david.func_test.widget.DiscoverTabFloatWidget
import com.david.func_test.widget.DragFloatWidget
import com.google.android.material.bottomsheet.BottomSheetBehavior

class AnimTestActivity:Activity() {
    lateinit var rightImg:ImageView
    lateinit var param: FrameLayout.LayoutParams
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_anim_test)
//        bottomSheet = findViewById(R.id.bottom_sheet)

//        testNormalFloat()
        testTabFloat()
//        testDragFloat()
//        findViewById<View>(R.id.anim_img)
    }

    private fun testDragFloat(){
        rightImg = findViewById(R.id.right_img)
        param = rightImg.layoutParams as FrameLayout.LayoutParams

        val tabFloatWidget = DragFloatWidget(findViewById(R.id.bottom_drag_sheet),  findViewById(R.id.title), this)

        rightImg.setOnClickListener {
            tabFloatWidget.animToState(BottomSheetBehavior.STATE_HALF_EXPANDED)
        }
    }

    private fun testTabFloat(){
        rightImg = findViewById(R.id.right_img)
        param = rightImg.layoutParams as FrameLayout.LayoutParams

        val tabFloatWidget = DiscoverTabFloatWidget(findViewById(R.id.bottom_tab_sheet),  findViewById(
            R.id.title
        ), this)

        rightImg.setOnClickListener {
            tabFloatWidget.animToState(BottomSheetBehavior.STATE_HALF_EXPANDED)
        }
    }


    private fun testNormalFloat(){
        rightImg = findViewById(R.id.right_img)

        param = rightImg.layoutParams as FrameLayout.LayoutParams
//        floatWidget = DiscoverFloatWidget(findViewById(R.id.bottom_sheet), findViewById(R.id.title), 0, this)
//        floatWidget.bottomSheetCallback = object :DiscoverBottomSheetCallback{
//            override fun onDragging(
//                bottomSheet: View,
//                eachDistance: Float,
//                offset: Float,
//                parentHeight: Int,
//                currentState: Int
//            ) {
//                if(!(currentState == STATE_HALF_EXPANDED && offset < -floatWidget.changeOffset) //半屏向上时且大于changeOffset时不跟随
//                    &&
//                    !(currentState == STATE_EXPANDED && offset >= 0)//全屏向下时也不跟随
//                ){
//                    param.bottomMargin = (parentHeight + 10)
//                    rightImg.layoutParams = param
//                }
//            }
//
//            override fun onFling(bottomSheet: View, eachDistance: Int, parentHeight: Int, newState: Int) {
//                if(
//                    newState != STATE_EXPANDED //执行动画向全屏时不跟随
//                    && !(newState == STATE_HALF_EXPANDED && parentHeight > (floatWidget.halfHeight + floatWidget.changeOffset))//全屏向半屏执行动画时且在changeOffset范围外时也不跟随
//                ) {
//                    param.bottomMargin = (parentHeight + 10)
//                    rightImg.layoutParams = param
//
//                }
//            }
//
//            override fun onAnimEnd(bottomSheet: View, parentHeight: Int, newState: Int) {
//            }
//
//            override fun onHeaderClick(bottomSheet: View, parentHeight: Int, currentState: Int) {
//                floatWidget.animToState(if (currentState == STATE_EXPANDED) STATE_HALF_EXPANDED else if(currentState == STATE_HIDDEN) STATE_HALF_EXPANDED else STATE_HIDDEN)
//            }
//
//        }

        val floatWidget = DiscoverFloatWidget(findViewById(R.id.bottom_sheet),  findViewById(R.id.title), this)
        floatWidget.bottomSheetCallback = object : DiscoverBottomSheetCallback {
            override fun onDragging(bottomSheet: View, listHeight: Int,
                                    currentState: Int) {
                if(listHeight > (floatWidget.halfHeight + floatWidget.changeOffset)){
                    return
                } else if (listHeight < 50) {//高度小于50时不在设置
                    return
                }
                param.bottomMargin = (listHeight + 10)
                rightImg.layoutParams = param
            }

            override fun onFling(bottomSheet: View, listHeight: Int,
                                 newState: Int) {
                if(listHeight > (floatWidget.halfHeight + floatWidget.changeOffset)){
                    return
                } else if (listHeight < 50) {
                    return
                }
                param.bottomMargin = (listHeight + 10)
                rightImg.layoutParams = param
            }

            override fun onAnimEnd(bottomSheet: View, parentHeight: Int, newState: Int) {
            }

            override fun onHeaderClick(bottomSheet: View, listHeight: Int, currentState: Int) {
            }

        }
        rightImg.setOnClickListener {
            floatWidget.animToState(BottomSheetBehavior.STATE_HALF_EXPANDED)
        }
//        val title = findViewById<View>(R.id.title)
    }
}
